package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import utils.db.impl.EditUser;

/**
 * Servlet implementation class ChangeTypeServlet
 */
@WebServlet("/ChangeTypeServlet")
public class ChangeTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    HttpSession session;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String type = request.getParameter("type");
		
		if(id!=0 && type!=""){
			EditUser edit=new EditUser();
			edit.changeType(id, type);
			session = request.getSession(true);
			session.setAttribute("warning", "Typ użytkownika został zmieniony!");
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location", "index.jsp?page=adminPanelUsers#add");
		}else{
			session = request.getSession(true);
			session.setAttribute("warning", "Błąd podczas zmiany typu użytkownika!");
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location", "index.jsp?page=adminPanelUsers#add");
		}
	}

}
